package com.shjn.squirrel.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shjn.squirrel.R;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 用户协议书对话框
 */
public class UserProtoDialog extends BaseDialogFragment implements View.OnClickListener {

    private static UserProtoDialog instance;
    private Thread mThread;
    private TextView mTvProtocol;

    public UserProtoDialog() {
    }

    public synchronized static UserProtoDialog getInstance() {
        if (instance == null) {
            instance = new UserProtoDialog();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //设置对话框资源文件
        setLayoutResource(R.layout.user_protocol_dialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        //设置对话框大小
        setdialogSize(333, 367, 0);
        //初始化控件
        ((Button) mDialog.findViewById(R.id.btn_ok)).setOnClickListener(this);
        mTvProtocol = (TextView) mDialog.findViewById(R.id.tv_protocol);
        setProtoText();
        //默认120秒自动关闭对话框
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(120000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mDialog.cancel();
                        mDialog.dismiss();
                        if (mOnCloseDialogListener != null) {
                            mOnCloseDialogListener.onCloseDialog();
                        }
                    }
                });
            }
        });
        mThread.start();
        super.onResume();
    }

    /**
     * 设置用户协议文本
     */
    private void setProtoText() {
        InputStream inputStream = null;
        String text = null;
        try {
            inputStream = getActivity().getAssets().open("user_protocol.txt");
            text = IOUtils.toString(inputStream, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mTvProtocol.setText(text);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //确定
            case R.id.btn_ok:
                closeDialog();
                break;
            default:
                break;
        }
    }

    /**
     * 关闭对话框
     */
    public void closeDialog() {
        if (mThread != null) {
            mThread.interrupt();
        }
    }
}
